<?php
/**
 * User: Drery
 * Date: 2015/12/30
 * Time: 14:15
 */

namespace backend\models;

use Yii;
use yii\db\ActiveRecord;

class Profile extends ActiveRecord
{
    public $password;
    public $password_repeat;

    public function rules()
    {
        return [
            [['password','password_repeat'],'required'],
            ['password','string','length'=>[6,16]],
            ['password','match','pattern'=>'/[0-9]{1}/','message'=>'密码至少包含一个数字'],
            ['password','match','pattern'=>'/[a-z]{1}/','message'=>'密码至少包含一个小写字母'],
            ['password','match','pattern'=>'/[A-Z]{1}/','message'=>'密码至少包含一个大写字母'],
            ['password_repeat','compare','compareAttribute'=>'password','message'=>'两次输入密码不一致']
        ];
    }

    public function attributeLabels()
    {
        return [
            'password' => '密码',
            'password_repeat' => '确认密码'
        ];
    }

    public function execute()
    {
        if ($this->validate()) {
            /** @var Admin $user_model */
            $user_model = Yii::$app->user->identity;
            $user_model->password = $this->password;
            $user_model->generatePassword();
            return $user_model->save();
        }
        return false;
    }
}